package com.example.billbillbangumi;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText id,password;
    private MyDatabaseHelper dbHelper;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView image=(ImageView)findViewById(R.id.image);
        image.setImageResource(R.drawable.login2);
        Button button1=(Button)findViewById(R.id.button1);
        id=(EditText)findViewById(R.id.id);
        password=(EditText)findViewById(R.id.password);
        password.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    image.setImageResource(R.drawable.login1);
                } else {
                    image.setImageResource(R.drawable.login2);
                }
            }
        });
        dbHelper=new MyDatabaseHelper(this,"Bangumi.db",null,12);
        db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("name","入间同学入魔了");
        values.put("starttime","20191005");
        values.put("updatetime","星期六");
        values.put("attention","未追番");
        db.insert("bangumi",null,values);
        values.clear();
        values.put("name","厨病激发男孩");
        values.put("starttime","20191004");
        values.put("updatetime","星期五");
        values.put("attention","未追番");
        db.insert("bangumi",null,values);
        values.clear();
        values.put("name","Dr.STONE石纪元");
        values.put("starttime","20190705");
        values.put("updatetime","星期五");
        values.put("attention","未追番");
        db.insert("bangumi",null,values);
        values.clear();
        values.put("name","花牌情缘3");
        values.put("starttime","20191023");
        values.put("updatetime","星期三");
        values.put("attention","未追番");
        db.insert("bangumi",null,values);
        values.clear();
        values.put("name","放学后桌游俱乐部");
        values.put("starttime","20191003");
        values.put("updatetime","星期四");
        values.put("attention","未追番");
        db.insert("bangumi",null,values);
        values.clear();
        values.put("id","123");
        values.put("password","111");
        db.insert("id",null,values);
        values.clear();
        values.put("id","321");
        values.put("password","222");
        db.insert("id",null,values);
        values.clear();
        values.put("id","111");
        values.put("password","123");
        db.insert("id",null,values);
        values.clear();
        values.put("id","222");
        values.put("password","321");
        db.insert("id",null,values);
        values.clear();
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if(id.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this,"请输入你的账号",Toast.LENGTH_SHORT).show();
                }
                else if(password.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }
                else{
                    Cursor cursor=db.rawQuery("select* from id where id=?",new String[]{id.getText().toString()});
                    if(cursor.moveToFirst()){
                        String tpassword=cursor.getString(cursor.getColumnIndex("password"));
                        if(password.getText().toString().equals(tpassword)){
                            Intent intent=new Intent(MainActivity.this,ButtonActivity.class);
                            startActivity(intent);
                        }
                        else{
                            Toast.makeText(MainActivity.this,"密码错误，请重新输入",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else {
                        Toast.makeText(MainActivity.this,"该账号不存在，请重新输入",Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
        Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                id.setText("");
                password.setText("");
            }
        });
        Button button3=(Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}
